<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>品牌管理</title>

    <style>
        #app {
            width: 600px;
            margin: 10px auto;
        }
        
        .tb {
            border-collapse: collapse;
            width: 100%;
        }
        
        th {
            background-color: #0094ff;
            color: white;
        }
        
        .tb td,
        .tb th {
            padding: 5px;
            border: 1px solid black;
            text-align: center;
        }
        
        .add {
            padding: 5px;
            border: 1px solid black;
            margin-bottom: 10px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="add">
            编号:<input type="text"> 品牌名称:
            <input type="text" v-model="name">
            <input type="button" value="添加" @click="addDate">
        </div>

        <div class="add">
            品牌名称:<input type="text" placeholder="请输入搜索条件">
        </div>

        <div>
            <table class="tb">
                <tr>
                    <th>编号</th>
                    <th>品牌名称</th>
                    <th>创立时间</th>
                    <th>操作</th>
                </tr>
                <tr v-if="list.length<=0">
                    <td colspan="4">没有品牌数据</td>
                </tr>
                <tr v-for="item in list" :key="item.id">
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.ctime}}</td>
                    <td>
                        <a href="javascript:void(0)" @click="del(item.id)">删除</a>
                    </td>
                </tr>

            </table>
        </div>
    </div>
</body>

</html>
<script src="vue.js"></script>
<script src="axios.js"></script>
<!-- <script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script> -->
<!-- <script src="https://cdn.bootcss.com/axios/0.18.0/axios.js"></script> -->
<script>
    new Vue({
        el: "#app",
        data: {
            list: [],
            name: "",
        },
        methods: {
            // 获取列表
            getlist: function() {
                var url = "http://157.122.54.189:9093/api/getprodlist";
                axios.get(url)
                    .then(rep => {
                        if (rep.data.status == 1) {
                            return;
                        }

                        this.list = rep.data.message;
                    });
            },
            //添加：
            addDate() {
                var url = "http://157.122.54.189:9093/api/addproduct";
                axios.post(url, {
                    name: this.name,
                }).then(rep => {
                    if (rep.data.status == 1) {
                        return;
                    }
                    alert("添加成功");
                    this.getlist(); //添加成功后重新渲染列表

                })
            },
            //删除
            del(id) {
                var url = "http://157.122.54.189:9093/api/delproduct/" + id;
                axios.get(url).then(rep => {
                    if (rep.data.status == 1) {
                        return;
                    }
                    alert("删除成功");
                    this.getlist(); //删除成功后重新渲染列表
                });
            }
        },
        mounted: function() {
            this.getlist();
        },
    })
</script>